// Term Project -- Saving Calculator
// Programer: Ying Wang

#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

// Function that collects data from the user and calculate the total monthly expense
double LessSpending (){					
	double E_Expense, B_Expense, U_Expense, Spendless;
	
	// ask inputs from the user
	cout << "1. Spend less on Entertainment? Enter amount here: $";
	cin >> E_Expense;
	cout << "2. Spend less on Budgets? Enter amount here: $";
	cin >> B_Expense;
	cout << "3. Spend less on Utilityies? Enter amount here: $";
	cin >> U_Expense;
	
	// calculate total reduction of Monthly expense
	Spendless = E_Expense + B_Expense + U_Expense;
	cout << "Your Monthly Spending decrease by $" << fixed << setprecision (2)
	<< Spendless << endl;
	
	return Spendless;			// return the variable to main function for further calculation
}	

void SpendingCalculator()        // main function
{
	cout << "\n                   Spending Calculator " << endl;
	cout << "------------------------------------------------------------" << endl;
	
	int nper;
	double AnnualR, Federal_TaxR, State_TaxR;
	double MonthlySaving;
	double SavingsAT;
	char again;
	
	// ask the user's willingness if he or she want to run the program again
	do {
		double SavingsBT = 0;

		MonthlySaving = LessSpending ();
		
		cout << "Enter number of years you plan to save: ";
		cin >> nper;
		cout << "Enter the Annual Rate of Return: ";
		cin >> AnnualR;
		cout << "Enter the Federal Tax Rate here: ";
		cin >> Federal_TaxR;
		cout << "Enter the State Tax Rate here: ";
		cin >> State_TaxR;
		
		// calculate the amount the user had saved 
		for (int i = 1; i <= 12*nper; i++) {
			SavingsBT = MonthlySaving * (pow ((1 + AnnualR/12), i)) + SavingsBT;
		}
		
		cout << "You saved: $" << SavingsBT << " before tax." << endl;
		
		// find the correct formula
		SavingsAT = SavingsBT - (SavingsBT - MonthlySaving * nper * 12) * (Federal_TaxR + State_TaxR);
		
		cout << "You saved: $" << SavingsAT << " after tax applied." << endl;
		cout << "Do you want to try agin? (y/n): ";
		cin >> again;
		cout << endl;
		
		}while (again == 'y' || again == 'Y');
		
	if (again == 'n' || again == 'N') {
		cout << "--- Thank You for using Spending Calculator! ---" << endl;
	}
	
	system ("Pause");
}